package com.example.controller;

import com.example.dto.SentenceAndTopic;
import com.example.entity.Topic;
import com.example.service.SentenceService;
import com.example.dto.Result;
import com.example.entity.Sentence;
import com.example.service.TopicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@RestController
@RequestMapping("/sentence")
@Api("对句子表的操作")
public class SentenceController {

    @Autowired
    private SentenceService sentenceService;
    @Autowired
    private TopicService topicService;

    @RequestMapping(value = "/getSentence", method = RequestMethod.GET)
    @ApiOperation(value = "获取所有句子信息", notes = "返回的是Result类型，数据段是所有句子的json格式")
    public Result<List<Sentence>> getSentence(){
        Result<List<Sentence>> result = new Result<>();
        List<Sentence> list = sentenceService.getAllSentences();
        return result.ok("所有句子", list);
    }

    @RequestMapping(value = "/getSentenceBySentenceId", method = RequestMethod.POST)
    @ApiOperation(value = "获取句子信息", notes = "返回的是Result类型，数据段是句子的json格式")
    public Result<Sentence> getSentenceBySentenceId(@RequestParam("sentenceId") String sentenceId){
        Result<Sentence> result = new Result<>();
        Sentence sentence = sentenceService.getSentenceById(sentenceId);
        return result.ok("句子信息", sentence);
    }

    @RequestMapping(value = "/getSentenceByTopicId", method = RequestMethod.POST)
    @ApiOperation(value = "通过主题Id来获取该主题所有句子", notes = "传递参数：用户id（userId），主题id（topicId）")
    public Result<SentenceAndTopic> getSentenceByTopicId(@RequestParam("userId") String userId, @RequestParam("topicId") String topicId){
        Result<SentenceAndTopic> result = new Result<>();
        SentenceAndTopic sentenceAndTopic = new SentenceAndTopic();
        List<Sentence> sentences = sentenceService.getSentenceByTopicIdSortByAcronym(userId, topicId);
        List<Sentence> sentences1 = sentenceService.getSentenceByTopicIdSortByTime(userId, topicId);
        Topic topic = topicService.getTopic(topicId, userId);
        sentenceAndTopic.setTopic(topic);
        sentenceAndTopic.setAllSentencesSortByAcronym(sentences);
        sentenceAndTopic.setAllSentencesSortByTime(sentences1);
        return result.ok("该主题所有句子", sentenceAndTopic);
    }

    @RequestMapping(value = "/setSentence", method = RequestMethod.POST)
    @ApiOperation(value = "管理员添加句子", notes = "该接口管理员调用，普通用户不允许调用")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "topicName", value = "主题名", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "sentenceContent", value = "句子内容",required = true, dataType = "String")
    })
    public void setSentence(@RequestParam("topicName") String topicName, @RequestParam("sentenceContent") String content){
        sentenceService.addSentence(topicName, content);
    }

    @RequestMapping(value = "/getStandardAudio", method = RequestMethod.POST)
    @ApiOperation(value = "获取句子标准音频", notes = "传递参数：句子id（sentenceId）")
    public Result<String> getStandardAudio(HttpServletRequest request, HttpServletResponse response, @RequestParam("sentenceId") String sentenceId) {
        Result<String> result = new Result<>();
        String path = sentenceService.getStandardAudio(sentenceId);
        return result.ok("标准音频地址", path);
    }

}
